import os
import json

import pandas as pd

from argparse import ArgumentParser

from generate_html import (
    HTMLGenerater, DespeckleInTurnHTMLGenerator, 
    InTurnHTMLGenerator
)

def parser():
    parser = ArgumentParser()
    parser.add_argument(
        '-f', '--folder', help='the folder under which the data save', default=""
    )
    parser.add_argument(
        '-d', '--default_folder', help='the folder under which the org data (such as patch and gt map) save', default=""
    )
    parser.add_argument(
        '-n', '--name'
    )
    args = parser.parse_args()

    return args


def main(fields, list_file, save_path, data_folder, fixed_data):

    # data_folder = args.folder
    # fixed_data = args.default_folder

    HG = InTurnHTMLGenerator(data_folder=data_folder, fixed_data=fixed_data)
    # HG = HTMLGenerater(data_folder=data_folder, fixed_data=fixed_data)
    # HG = DespeckleInTurnHTMLGenerator(data_folder=data_folder, fixed_data=fixed_data)
    HG.preprocess()

    # fields = ["l2=dis",]
    # filepath = "E:/workspace/SOMatch/json/os_dataset/256_val.json"
    # with open(filepath, 'r') as fp:
    #     list_file = json.load(fp)
    # list_file = list_file["OPT"]


    fd = HG.folder_dict
    columns = ["iter", "batch"]
    columns.extend(fields)

    df = pd.DataFrame(data=None, columns=columns)

    ids = 1
    for i in fd[fields[0]]:

        for b in fd[fields[0]][i]:

            record = [int(i), int(b)]

            for m in fields:
                record.append(fd[m][i][b])

            df.loc[ids] = record
            ids += 1

    df = df.sort_values(by=["iter", "batch"])
    cols = df.columns.tolist()
    cols.insert(len(columns), "ID")
    cols.insert(len(columns), "path")
    df = df.reindex(columns=cols)

    cnt = 0
    for ids in df.index:
        df.loc[ids, "ID"] = cnt
        df.loc[ids, "path"] = list_file[cnt]
        cnt += 1

    df.to_excel(save_path, "Sheet1")


# data to excel
if __name__ == "__main__1":

    args = parser()

    # fields = ["SSIM", "PSNR"]
    fields = ["l2=dis", ]

    # filepath = "E:/workspace/SOMatch/json/despeckle/despeckle.json"
    filepath = "E:/workspace/SOMatch/json/os-select/1-s-test.json"
    with open(filepath, 'r') as fp:
        list_file = json.load(fp)
    list_file = list_file["OPT"]

    data_folder = args.folder
    fixed_data = args.default_folder
    save_path = os.path.join(
        "E:/workspace/SOMatch/tmp", 
        args.name)

    main(fields=fields, list_file=list_file, save_path=save_path, data_folder=data_folder, fixed_data=fixed_data)


if __name__ == "__main__1":
    file_dict = {
        "SSLC" : "E:/workspace/SOMatch/tmp/SSLC.xlsx", 
        "resddfncam3" : "E:/workspace/SOMatch/tmp/resddfncam3.xlsx"
    }

    df_dict = {}

    for k, v in file_dict.items():
        df_dict[k] = pd.read_excel(v, sheet_name="Sheet1")


# join L1~L8 excel
if __name__ == "__main__1":

    file_dict = {
        "L1" : {
            "ID-CNN" : "E:/workspace/SOMatch/tmp/ID-CNN-L1.xlsx", 
            "SAR-DRN" : "E:/workspace/SOMatch/tmp/SAR-DRN-L1.xlsx", 
            "SC" : "E:/workspace/SOMatch/tmp/SC-L1.xlsx", 
            "ME" : "E:/workspace/SOMatch/tmp/ME-L1.xlsx", 
        },
        "L2" : {
            "ID-CNN" : "E:/workspace/SOMatch/tmp/ID-CNN-L2.xlsx", 
            "SAR-DRN" : "E:/workspace/SOMatch/tmp/SAR-DRN-L2.xlsx", 
            "SC" : "E:/workspace/SOMatch/tmp/SC-L2.xlsx", 
            "ME" : "E:/workspace/SOMatch/tmp/ME-L2.xlsx", 
        },
        "L4" : {
            "ID-CNN" : "E:/workspace/SOMatch/tmp/ID-CNN-L4.xlsx", 
            "SAR-DRN" : "E:/workspace/SOMatch/tmp/SAR-DRN-L4.xlsx", 
            "SC" : "E:/workspace/SOMatch/tmp/SC-L4.xlsx", 
            "ME" : "E:/workspace/SOMatch/tmp/ME-L4.xlsx", 
        },
        "L8" : {
            "ID-CNN" : "E:/workspace/SOMatch/tmp/ID-CNN-L8.xlsx", 
            "SAR-DRN" : "E:/workspace/SOMatch/tmp/SAR-DRN-L8.xlsx", 
            "SC" : "E:/workspace/SOMatch/tmp/SC-L8.xlsx", 
            "ME" : "E:/workspace/SOMatch/tmp/ME-L8.xlsx", 
        },
    }

    df_dict = {}

    for L in file_dict:
        df_dict[L] = {}

        for m, f in file_dict[L].items():
            df = pd.read_excel(f, sheet_name="Sheet1")
            df_dict[L][m] = df

    res = {}

    for L in df_dict:

        m_iter = iter(df_dict[L].keys())
        f_iter = iter(df_dict[L].values())
        length = len(df_dict[L])

        # valid when num of model is even number
        left_name = next(m_iter)
        right_name = next(m_iter)
        left_df = next(f_iter)
        right_df = next(f_iter)
        left_name = "_" + left_name
        right_name = "_" + right_name

        # res[L] = left_df.join(right_df, on="ID", how="inner", lsuffix=left_name, rsuffix=right_name)
        res[L] = left_df.set_index("ID").merge(right_df.set_index("ID"), on=("ID", "path"), how="inner", suffixes=(left_name, right_name))

        left_name = right_name

        for i in range(2, length):

            right_name = "_" + next(m_iter)
            right_df = next(f_iter)

            # res[L] = res[L].join(right_df, on="ID", how="inner", lsuffix=left_name, rsuffix=right_name)
            res[L] = res[L].merge(right_df.set_index("ID"), on=("ID", "path"), how="inner", suffixes=(left_name, right_name))
            
            left_name = right_name

        res[L].to_excel(f"./tmp/{L}.xlsx", "Sheet1")


# L1 ~ L8 select validate data
if __name__ == "__main__1":

    file_list = ["L1", "L2", "L4", "L8"]

    for f in file_list:
        df = pd.read_excel(f"./tmp/{f}.xlsx", sheet_name="Sheet1")
        df_diff = df
        df_diff["ME-SC"] = df_diff["SSIM_ME"]-df_diff["SSIM_SC"]
        df_diff["SC-SD"] = df_diff["SSIM_SC"]-df_diff["SSIM_SAR-DRN"]
        df_diff["SD-ID"] = df_diff["SSIM_SAR-DRN"]-df_diff["SSIM_ID-CNN"]
        df_validate = df_diff[df_diff["SD-ID"]>0][df_diff["SC-SD"]>0][df_diff["ME-SC"]>0]
        df = df_validate.sort_values(by="ME-SC", ascending=False)
        df.to_excel(f"./tmp/{f}-diff.xlsx", "Sheet1")


# ENL data to excel
if __name__ == "__main__1":

    args = parser()

    fields = ["ENL"]

    filepath = "E:/workspace/SOMatch/json/os_dataset/512_test.json"
    with open(filepath, 'r') as fp:
        list_file = json.load(fp)
    list_file = list_file["SAR"]

    data_folder = args.folder
    fixed_data = args.default_folder
    save_path = os.path.join(
        "E:/workspace/SOMatch/tmp/enl", 
        args.name)

    main(fields=fields, list_file=list_file, save_path=save_path, data_folder=data_folder, fixed_data=fixed_data)


# template match data to excel
if __name__ == "__main__1":

    args = parser()

    fields = ["l2=dis"]

    filepath = "E:/workspace/SOMatch/json/os_dataset/256.json"
    with open(filepath, 'r') as fp:
        list_file = json.load(fp)
    list_file = list_file["OPT"]

    data_folder = args.folder
    fixed_data = args.default_folder
    save_path = os.path.join(
        "E:/workspace/SOMatch/tmp/os-select", 
        args.name)

    main(fields=fields, list_file=list_file, save_path=save_path, data_folder=data_folder, fixed_data=fixed_data)


# join os-select df
if __name__ == "__main__1":
    file_list = [
        "E:/workspace/SOMatch/tmp/os-select/s1.xlsx", 
        "E:/workspace/SOMatch/tmp/os-select/s2.xlsx", 
        "E:/workspace/SOMatch/tmp/os-select/s3.xlsx", 
        "E:/workspace/SOMatch/tmp/os-select/s4.xlsx", 
    ]
    df_list = []

    for f in file_list:

        df = pd.read_excel(f, sheet_name="Sheet1").set_index("ID")
        df_list.append(df)

    length = len(file_list)
    suffix = iter(list(range(length)))
    f_iter = iter(df_list)

    left_df = next(f_iter)
    right_df = next(f_iter)
    left_name = "_" + str(next(suffix))
    right_name = "_" + str(next(suffix))

    res = left_df.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))

    left_name = right_name

    for i in range(2, length):

        right_name = "_" + str(next(suffix))
        right_df = next(f_iter)
        res = res.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))
        left_name = right_name

    res.to_excel("./tmp/os-select/res.xlsx", "Sheet1")


if __name__ == "__main__1":

    fn = "./tmp/os-select/res.xlsx"
    df = pd.read_excel(fn, sheet_name="Sheet1")
    thresh = 6

    df = df[df["l2=dis_0"] <= thresh]
    df = df[df["l2=dis_1"] <= thresh]
    df = df[df["l2=dis_2"] <= thresh]
    df = df[df["l2=dis_3"] <= thresh]

    df.to_excel("./tmp/os-select/res-select.xlsx", "Sheet1")


# join enl df
if __name__ == "__main__1":
    file_list = [
        "E:/workspace/SOMatch/tmp/enl/ID-CNN_enl_res.xlsx", 
        "E:/workspace/SOMatch/tmp/enl/SAR-DRN_enl_res.xlsx", 
        "E:/workspace/SOMatch/tmp/enl/SAR-CAM_enl_res.xlsx", 
        "E:/workspace/SOMatch/tmp/enl/ms_enl_res.xlsx"
    ]
    df_list = []

    for f in file_list:

        df = pd.read_excel(f, sheet_name="Sheet1").set_index("ID")
        df_list.append(df)

    length = len(file_list)
    suffix = iter(list(range(length)))
    f_iter = iter(df_list)

    left_df = next(f_iter)
    right_df = next(f_iter)
    left_name = "_" + str(next(suffix))
    right_name = "_" + str(next(suffix))

    res = left_df.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))

    left_name = right_name

    for i in range(2, length):

        right_name = "_" + str(next(suffix))
        right_df = next(f_iter)
        res = res.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))
        left_name = right_name

    res.to_excel("./tmp/enl/res.xlsx", "Sheet1")


# L1 ~ L8 select validate data
if __name__ == "__main__1":

    
    df = pd.read_excel("E:/workspace/SOMatch/tmp/enl/res.xlsx", sheet_name="Sheet1")
    df_diff = df
    df_diff["ME-SC"] = df_diff["ENL_3"]-df_diff["ENL_2"]
    df_diff["ME-SD"] = df_diff["ENL_3"]-df_diff["ENL_1"]
    df_diff["ME-ID"] = df_diff["ENL_3"]-df_diff["ENL_0"]
    df_validate = df_diff[df_diff["ME-ID"]>0][df_diff["ME-SD"]>0][df_diff["ME-SC"]>0]
    df = df_validate.sort_values(by="ME-SC", ascending=False)
    df.to_excel(f"./tmp/enl/res-sort.xlsx", "Sheet1")


# join template-match-val df
if __name__ == "__main__1":
    file_list = [
        "E:/workspace/SOMatch/tmp/Tmatch-val/sslc-tm.xlsx", 
        "E:/workspace/SOMatch/tmp/Tmatch-val/ddfn-tm.xlsx", 
        "E:/workspace/SOMatch/tmp/Tmatch-val/unet-tm.xlsx", 
        "E:/workspace/SOMatch/tmp/Tmatch-val/INTEG-tm.xlsx", 
    ]
    df_list = []

    for f in file_list:

        df = pd.read_excel(f, sheet_name="Sheet1").set_index("ID")
        df_list.append(df)

    length = len(file_list)
    suffix = iter(list(range(length)))
    f_iter = iter(df_list)

    left_df = next(f_iter)
    right_df = next(f_iter)
    left_name = "_" + str(next(suffix))
    right_name = "_" + str(next(suffix))

    res = left_df.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))

    left_name = right_name

    for i in range(2, length):

        right_name = "_" + str(next(suffix))
        right_df = next(f_iter)
        res = res.merge(right_df, on=("ID", "path"), how="inner", suffixes=(left_name, right_name))
        left_name = right_name

    res.to_excel("E:/workspace/SOMatch/tmp/Tmatch-val/res.xlsx", "Sheet1")


# template-match-val select
if __name__ == "__main__":

    
    df = pd.read_excel("E:/workspace/SOMatch/tmp/Tmatch-val/res.xlsx", sheet_name="Sheet1")
    df_diff = df
    df_diff["INTEG-SSLC"] = df_diff["l2=dis_3"]-df_diff["l2=dis_0"]
    df_diff["SSLC-DDFN"] = df_diff["l2=dis_0"]-df_diff["l2=dis_1"]
    df_diff["DDFN-UNet"] = df_diff["l2=dis_1"]-df_diff["l2=dis_2"]
    df_validate = df_diff[df_diff["INTEG-SSLC"]<0][df_diff["SSLC-DDFN"]<0][df_diff["DDFN-UNet"]<0]
    df = df_validate.sort_values(by="INTEG-SSLC", ascending=False)
    df.to_excel(f"E:/workspace/SOMatch/tmp/Tmatch-val/res-sort.xlsx", "Sheet1")

